package org.stvd.service.admin.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.stvd.common.SecurityUserHolder;
import org.stvd.common.utils.ServiceResult;
import org.stvd.core.dto.QueryResult;
import org.stvd.core.util.DateUtil;
import org.stvd.core.util.StringUtil;
import org.stvd.entities.admin.Dictionary;
import org.stvd.entities.admin.DictionaryType;
import org.stvd.repository.admin.DictionaryDao;
import org.stvd.repository.admin.DictionaryTypeDao;
import org.stvd.service.admin.DictionaryService;
import org.stvd.service.base.impl.BaseServiceImpl;

@Service("dictionaryService")
public class DictionaryServiceImpl extends BaseServiceImpl<Dictionary>
    implements DictionaryService {

    @Resource(name = "DictionaryDao")
    private DictionaryDao dictionaryDao;
    @Resource(name = "DictionaryTypeDao")
    private DictionaryTypeDao dictionaryTypeDao;

    @Override
    public ServiceResult<Object> insertDictionaryType(DictionaryType dictionaryType) {
        ServiceResult<Object> serviceResult = new ServiceResult<Object>();
        if(dictionaryType==null) {
            serviceResult.setParamError();
            return serviceResult;
        }
        DictionaryType oldDictionaryType = dictionaryTypeDao.findByPk(DictionaryType.class, dictionaryType.getId());
        if(oldDictionaryType!=null) {
            serviceResult.setDataError("当前类别ID已存在！");
            return serviceResult;
        }
        dictionaryType.setCreateUser(SecurityUserHolder.getCurrentUserid());
        dictionaryType.setCreateTime(DateUtil.getSystemDate());
        dictionaryTypeDao.insert(dictionaryType);
        serviceResult.setMessage("新增成功");
        return serviceResult;
    }
    
    @Override
    @Transactional
    public ServiceResult<Object> updateDictionaryType(DictionaryType dictionaryType) {
        ServiceResult<Object> serviceResult = new ServiceResult<Object>();
        if(dictionaryType==null) {
            serviceResult.setParamError();
            return serviceResult;
        }
        if(!dictionaryType.getId().equals(dictionaryType.getOldId())){
            DictionaryType oldDictionaryType = dictionaryTypeDao.findByPk(DictionaryType.class, dictionaryType.getId());
            if(oldDictionaryType!=null) {
                serviceResult.setDataError("当前类别ID已存在！");
                return serviceResult;
            }else {
                dictionaryDao.updateDictionaryType(dictionaryType.getId(), dictionaryType.getOldId());
            }
        }
        dictionaryType.setModifyTime(DateUtil.getSystemDate());
        dictionaryTypeDao.update(dictionaryType);
        serviceResult.setMessage("修改成功");
        return serviceResult;
    }
    
    @Override
    public ServiceResult<Object> deleteDictionaryType(String typeId) {
        ServiceResult<Object> serviceResult = new ServiceResult<Object>();
        if(StringUtil.isEmpty(typeId)) {
            serviceResult.setParamError();
            return serviceResult;
        }
        List<Dictionary> list = dictionaryDao.listDictionaryByType(typeId);
        if(list!=null && list.size() > 0) {
            serviceResult.setDataError("禁止删除，当前类别下存在字典数据！");
            return serviceResult;
        }
        dictionaryTypeDao.delete(DictionaryType.class, typeId);
        serviceResult.setMessage("删除成功");
        return serviceResult;
    }
    
    @Override
    public ServiceResult<Object> insertDictionary(Dictionary dictionary) {
        ServiceResult<Object> serviceResult = new ServiceResult<Object>();
        if(dictionary==null) {
            serviceResult.setParamError();
            return serviceResult;
        }
        Dictionary oldDictionary = dictionaryDao.findByTypeID(dictionary.getType(), dictionary.getId());
        if(oldDictionary!=null) {
            serviceResult.setDataError("当前字典数据已存在");
            return serviceResult;
        }
        dictionary.setCreateUser(SecurityUserHolder.getCurrentUserid());
        dictionary.setCreateTime(DateUtil.getSystemDate());
        dictionaryDao.insert(dictionary);
        
        serviceResult.setMessage("新增成功");
        return serviceResult;
    }

    @Override
    public ServiceResult<Object> updateDictionary(Dictionary dictionary) {
        ServiceResult<Object> serviceResult = new ServiceResult<Object>();
        if(dictionary==null) {
            serviceResult.setParamError();
            return serviceResult;
        }
        if(!dictionary.getId().equals(dictionary.getOldId())){
            Dictionary oldDictionary = dictionaryDao.findByTypeID(dictionary.getType(), dictionary.getId());
            if(oldDictionary!=null) {
                serviceResult.setDataError("当前字典数据已存在");
                return serviceResult;
            }
            dictionaryDao.updateDictionaryUpperId(dictionary.getOldId(), dictionary.getId());
            dictionaryDao.deleteDictionary(dictionary.getType(), dictionary.getOldId());
        }
        dictionary.setModifyTime(DateUtil.getSystemDate());
        dictionaryDao.update(dictionary);
        
        serviceResult.setMessage("修改成功");
        return serviceResult;
    }

    @Override
    public ServiceResult<Object> deleteDictionary(String typeId, String dicId) {
        ServiceResult<Object> serviceResult = new ServiceResult<Object>();
        if(StringUtil.isEmpty(typeId) || StringUtil.isEmpty(dicId)) {
            serviceResult.setParamError();
            return serviceResult;
        }
        List<Dictionary> list= dictionaryDao.listDictionaryByTypeUpperId(typeId, dicId);
        if(list!=null && list.size() > 0) {
            serviceResult.setDataError("禁止删除，当前数据存在子集");
            return serviceResult;
        }
        dictionaryDao.deleteDictionary(typeId, dicId);
        serviceResult.setMessage("删除成功");
        return serviceResult;
    }
    
    @Override
    public QueryResult<DictionaryType> queryDictionaryTypeResult(int start, int limit, String id, String name) {
        return dictionaryTypeDao.queryDictionaryTypeResult(start, limit, id, name);
    }
    
    @Override
    public List<DictionaryType> findAllDictionaryType() {
        return dictionaryTypeDao.findAll(DictionaryType.class);
    }

    @Override
    public List<Dictionary> getQueryByType(String type) {
        return dictionaryDao.listDictionaryByType(type);
    }

    @Override
    public List<Map<String, Object>> listAllDicsByType(String type) {
        return dictionaryDao.listAllDicsByType(type);
    }

    @Override
    public List<Dictionary> findAllUpperDicsByType(String type) {
        return dictionaryDao.findAllUpperDicsByType(type);
    }

    @Override
    public Dictionary findByTypeID(String type, String id) {
        return dictionaryDao.findByTypeID(type, id);
    }

    @Override
    public DictionaryType findByTypeID(String typeId) {
        return dictionaryTypeDao.findByPk(DictionaryType.class, typeId);
    }
}
